package com.desaysv.someip

import java.util.*

/**
 * @author uidq5208
 * @date 2023-3-14
 */
object PIVIManager {

    const val CMD_PIVI_ACCOUNT_LOGIN_STATE = 0x001
    const val CMD_PIVI_GET_USER_ID = 0x002
    const val CMD_PIVI_OPEN_LOGIN_HMI = 0x003
    const val CMD_PIVI_LOGIN_STATE_NOTIFY = 0x004

    const val RESP_USER_LOGGED_OUT = 0//没有用户登录
    const val RESP_USER_CONFIRMED = 1//已选择用户，但未验证
    const val RESP_USER_AUTHENTICATED = 2//用户已输入其InControl智能驭享帐户凭据/PIN

    //获取pivi登录状态请求失败
    const val RESP_APB_LOGIN_STATE_FAIL = 0b0001

    //获取pivi用户id失败
    const val RESP_APB_GET_USER_ID_FAIL = 0b0010

    interface CallBack {
        fun piviAuthSuccess(piviId: String)
        fun piviNotAuth()
        fun fail(failCode: Int)
    }

    private var mCallBacks = Vector<CallBack>()
    fun init(callBack: CallBack) {
        if (!mCallBacks.contains(callBack)) {
            mCallBacks.add(callBack)
        }
        SomeIpManager.getInstance().removeObserver(
            mSomeIpObserver
        )
        SomeIpManager.getInstance().addObserver(
            mSomeIpObserver
        )
    }

    private val mSomeIpObserver: SomeIpManager.SomeIpObserverImpl =
        object : SomeIpManager.SomeIpObserverImpl() {
            override fun response(messager: SomeIpMessager) {
                dealResponse(messager)
            }

            override fun timeout(methodId: Int, sessionId: Int) {
                for (mCallBack in mCallBacks) {
                    if (CMD_PIVI_GET_USER_ID == methodId) {
                        mCallBack.fail(RESP_APB_GET_USER_ID_FAIL)
                    } else {
                        mCallBack.fail(RESP_APB_LOGIN_STATE_FAIL)
                    }
                }
            }
        }

    /**
     * 请求PIVI登录状态
     */
    fun requestPIVILoginState() {
        SomeIpManager.getInstance().send(CMD_PIVI_ACCOUNT_LOGIN_STATE, null)
    }

    fun deInit() {
        SomeIpManager.getInstance().destroy()
        mCallBacks.removeAllElements()
    }

    /**
     * 请求PIVI的userId
     */
    private fun requestPIVIUserId() {
        SomeIpManager.getInstance().send(CMD_PIVI_GET_USER_ID, null)
    }

    /**
     * 跳转PIVI登录界面
     */
    fun loadPIVILoginHMI() {
        SomeIpManager.getInstance()
            .send(CMD_PIVI_OPEN_LOGIN_HMI, null, SomeIpMessager.MsgType.REQUEST_NO_RETURN)
    }

    /**
     * 处理PIVI消息响应
     */
    private fun dealResponse(messager: SomeIpMessager) {
        if (messager.methodId == CMD_PIVI_ACCOUNT_LOGIN_STATE && messager.messageType == SomeIpMessager.MsgType.RESPONSE) {
            //查询pivi登录状态的响应
            when (SomeIpMessager.byteToInt(messager.payload, false)) {
                //用户已登出
                RESP_USER_LOGGED_OUT -> {
//                    loadPIVILoginHMI()
                    for (mCallBack in mCallBacks) {
                        mCallBack.piviNotAuth()
                    }
                }
                //用户已登录
                RESP_USER_AUTHENTICATED -> {
                    requestPIVIUserId()
                }
                RESP_USER_CONFIRMED -> {
//                    loadPIVILoginHMI()
                    for (mCallBack in mCallBacks) {
                        mCallBack.piviNotAuth()
                    }
                }
            }
        } else if (messager.methodId == CMD_PIVI_GET_USER_ID && messager.messageType == SomeIpMessager.MsgType.RESPONSE) {
            //获取pivi用户id的响应
            if (messager.payload == null) {
                for (mCallBack in mCallBacks) {
                    mCallBack.fail(RESP_APB_GET_USER_ID_FAIL)
                }
            } else {
                val userId = String(messager.payload, Charsets.UTF_8)
                for (mCallBack in mCallBacks) {
                    mCallBack.piviAuthSuccess(userId)
                }
            }
        } else if (messager.methodId == CMD_PIVI_LOGIN_STATE_NOTIFY && messager.messageType == SomeIpMessager.MsgType.NOTIFICATION) {
            //通知消息只处理登录成功
            if (SomeIpMessager.byteToInt(
                    messager.payload,
                    false
                ) == RESP_USER_AUTHENTICATED
            ) {
                requestPIVIUserId()
            }
        }
    }
}